$(document).ready(function () {
	$('.login').click(function () {
		$('.dialog-mask').toggle()
	})
	$('.cancel-btn').click(function () {
		$('.dialog-mask').hide()
	})
	// 验证码状态
	let captchaState = {
		hash: '',
		url: ''
	};

	// 获取验证码
	function getCaptcha () {
		// 模拟API请求
		console.log('获取验证码');
		// 实际项目中替换为真实的API调用
		$.ajax({
			url: '/api/captcha',
			method: 'GET',
			success: function (data) {
				captchaState.hash = data.hash;
				$('#captchaImage').attr('src', data.base64);
			},
			error: function () {
				console.error('获取验证码失败');
			}
		});
	}

	// 点击验证码图片刷新
	$('#captchaImage').click(getCaptcha);

	// 登录
	$('#loginBtn').click(function () {
		const username = $('#username').val();
		const password = $('#password').val();
		const captcha = $('#captcha').val();

		// 简单验证
		if (!username || !password || !captcha) {
			alert('请填写完整信息');
			return;
		}

		// 模拟登录请求
		console.log('登录请求', {
			username,
			password,
			captcha,
			hash: captchaState.hash
		});

		// 实际项目中替换为真实的API调用
		$.ajax({
			url: '/api/login',
			method: 'POST',
			data: {
				username,
				password,
				captcha,
				hash: captchaState.hash
			},
			success: function (data) {
				alert('登录成功');
				// 设置cookie
				document.cookie = `token=${data.token}; path=/`;
				// 关闭弹窗
				$('#loginDialog').hide();
				// 刷新页面
				setTimeout(() => {
					location.reload();
				}, 100);
			},
			error: function () {
				alert('登录失败');
				getCaptcha();
			}
		});
	});

	$('.international').on('click', function (e) {
		// $.cookie('lang', 'en-us', { path: '/', expires: 10 });
		// window.location.reload();
		$('.poper').fadeToggle()
	})
	$('.cl-cn').on('click', function (e) {
		document.cookie = "lang=zh-cn;path=/;expires=10; secure"
		window.location.reload();
	})
	$('.cl-en').on('click', function (e) {
		document.cookie = "lang=en-us;path=/;expires=10; secure"
		window.location.reload();
	})

	// 初始化获取验证码
	getCaptcha();
});